<template>
  <div>
    <van-nav-bar fixed placeholder :title="key + '的搜索结果'" />
    <van-list v-model="loading" :finished="finished" @load="onLoad">
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title" @click="$router.push(`/article/${item.art_id}`)"/>
    </van-list>
  </div>
</template>

<script>
import { resultAPI } from '@/api/search'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      key: this.$route.query.key,
      page: 1
    }
  },
  methods: {
    async onLoad () {
      const res = await resultAPI({
        page: this.page,
        per_page: 10,
        q: this.key
      })
      this.list.push(...res.data.results)
      // 把本次加载状态结束
      this.loading = false
      // 让页码+1
      this.page++
      // 判断全部加载完成
      if (this.list.length >= res.data.total_count) {
        this.finished = true
      }
    }
  }
}
</script>

<style></style>
